/*
 * Copyright (C) 2019 ByteDance Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bytedance.scene.animation.animatorexecutor;


import com.bytedance.scene.Scene;
import com.bytedance.scene.animation.AnimationInfo;
import com.bytedance.scene.animation.NavigationAnimatorExecutor;
import com.bytedance.scene.animation.TransitionUtils;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;

/**
 * Created by JiangQi on 8/3/18.
 */
public class DefaultSceneAnimatorExecutor extends NavigationAnimatorExecutor {
    private DialogSceneAnimatorExecutor mDialogSceneAnimatorExecutor = new DialogSceneAnimatorExecutor();

    @Override
    public boolean isSupport(Class<? extends Scene> from, Class<? extends Scene> to) {
        return true;
    }

    @Override
    protected boolean disableConfigAnimationDuration() {
        return true;
    }

    
    @Override
    protected Animator onPushAnimator(AnimationInfo from, final AnimationInfo to) {
        if (to.mIsTranslucent) {
            return mDialogSceneAnimatorExecutor.onPushAnimator(from, to);
        }
        final Component fromView = from.mSceneView;
        final Component toView = to.mSceneView;

        AnimatorProperty fromAlphaAnimator=new AnimatorProperty();
        fromAlphaAnimator.setTarget(fromView)
                .alphaFrom(0.7f).alpha(1.0f)
                .setCurveType(Animator.CurveType.DECELERATE)
                .setDuration(120);

        AnimatorProperty toAlphaAnimator=new AnimatorProperty();
        toAlphaAnimator.setTarget(toView)
                .alphaFrom(0f).alpha(1.0f)
                .setCurveType(Animator.CurveType.CUBIC_BEZIER_DECELERATION)
                .setDuration(120);

        AnimatorProperty toTranslateAnimator=new AnimatorProperty();
        toTranslateAnimator.setTarget(toView)
                .moveFromY(0.08f * toView.getHeight()).moveToY(0f)
                .setCurveType(Animator.CurveType.CUBIC_BEZIER_DECELERATION)
                .setDuration(200);

        return TransitionUtils.mergeAnimators(fromAlphaAnimator, toAlphaAnimator, toTranslateAnimator);
    }

    
    @Override
    protected Animator onPopAnimator(final AnimationInfo fromInfo, final AnimationInfo toInfo) {
        if (fromInfo.mIsTranslucent) {
            return mDialogSceneAnimatorExecutor.onPopAnimator(fromInfo, toInfo);
        }
        final Component fromView = fromInfo.mSceneView;
        final Component toView = toInfo.mSceneView;

        AnimatorProperty fromAlphaAnimator=new AnimatorProperty();
        fromAlphaAnimator.setTarget(fromView)
                .alphaFrom(1.0f).alpha(0f)
                .setCurveType(Animator.CurveType.LINEAR)
                .setDuration(150).setDelay(50);

        AnimatorProperty fromTranslateAnimator=new AnimatorProperty();
        fromTranslateAnimator.setTarget(fromView)
                .moveFromY(0).moveToY(0.08f * toView.getHeight())
                .setCurveType(Animator.CurveType.CUBIC_BEZIER_ACCELERATION)
                .setDuration(200);


        AnimatorProperty toAlphaAnimator=new AnimatorProperty();
        toAlphaAnimator.setTarget(toView)
                .alphaFrom(0.7f).alpha(1.0f)
                .setCurveType(Animator.CurveType.CUBIC_BEZIER_EXTREME_DECELERATION)
                .setDuration(200);


        return TransitionUtils.mergeAnimators(fromAlphaAnimator,fromTranslateAnimator,toAlphaAnimator);
    }
}
